/*
 * alignContainerF.cpp
 *
 *  Created on: 2011-09-08
 *      Author: pawel
 */
#include"alignContainerF.h"
#include"align.h"
#include"backtrack/alignStrings.h"
#include"seqFeederRandom.h"
#include"seqFeederFile.h"
#include"algorithms.h"
#include"alignContainerScore.h"
#include"stat/simpleStat.h"
#include<Bpp/Seq/Sequence.h>
#include<string>
#include<iostream>
#include<memory>
#include<stdio.h>

//std::string alignContainerF::outSeparator = " ; ";
void alignContainerF::pushAlign(align* &align){
	if(align != NULL){
		 scoreList.push_front(align->getScore());
		 alignList.push_front(align);
		 align = NULL;
	}

}
const __gnu_cxx::slist<int>& alignContainerF::getScoreList()const{
	return (scoreList);
}
void alignContainerF::calculateStats(size_t simulations,optAlgThreadParam thparam,size_t threadsNum){
	align *aPtr;
	for(std::list<align*>::iterator it= alignList.begin();it != alignList.end();++it){
		aPtr = *it;
		std::auto_ptr<bpp::BasicSequence> smartSeqPtr(aPtr->lettersFreq.getSimpleSeq());
		seqFeederRandom randomSeqFeder(smartSeqPtr.get(),simulations);
		//seqFeederFile fil("/home/pawel/Projekt_inz/databases/uniprot_sprot.fasta/uniprot_sprot.fasta_t",AA);
		alignContainerScore scoreAlignContainer;
		thparam.alignContainerPtr = &scoreAlignContainer;
		thparam.seqFeederPtr = &randomSeqFeder;
		thparam.btrace = false;

		optAlgThreadRunner(thparam,threadsNum);

		simpleStat simpleStats(thparam.alignContainerPtr);
		aPtr->zScore = simpleStats.getZscore(aPtr->score);

	}
}
void alignContainerF::sortByScore(){
	alignList.sort(align::compareScore);
}
void alignContainerF::sortByIdentity(){
	alignList.sort(align::compareIdentity);
}
void alignContainerF::sortBySimilarity(){
	alignList.sort(align::compareSimilarity);
}
void alignContainerF::sortByGaps(){
	alignList.sort(align::compareGaps);
}
void alignContainerF::sortByEValue(){
	alignList.sort(align::compareEValue);
}
void alignContainerF::sortByZScore(){
	alignList.sort(align::compareZScore);
}
void alignContainerF::sortByBits(){
	alignList.sort(align::compareBits);

}
std::ostream& operator<<(std::ostream& out,alignContainerF& obj){

	align *aPtr;
	for(std::list<align*>::iterator it= obj.alignList.begin();it != obj.alignList.end();++it){
		aPtr = *it;
		std::string outSeparator = ";\t";
		out<<aPtr->getCompSeqName()<<outSeparator<<aPtr->getScore()<<outSeparator<<aPtr->getIdentity()<<outSeparator<<aPtr->getSimilarity()
		<<outSeparator<<aPtr->getGaps()
		<<outSeparator<<aPtr->getEValue()
		<<outSeparator<<aPtr->getZScore()

		<<outSeparator<<aPtr->getBits()
		<<std::endl;
		out<<std::string(aPtr->btrackedAlign.getTrimmedQStr())<<"("<<aPtr->btrackedAlign.getQueryAlignStart()<<")"
				<<"-("<<aPtr->btrackedAlign.getQueryAlignStop()<<")"<<"\n"
				<<std::string(aPtr->btrackedAlign.getTrimmedAStr())<<"\n"
				<<std::string(aPtr->btrackedAlign.getTrimmedDbStr())<<"("<<aPtr->btrackedAlign.getDbAlignStart()<<")"
				<<"-("<<aPtr->btrackedAlign.getDbAlignStop()<<")"
				<<std::endl;
	}
	return out;
}
alignContainerF::~alignContainerF(){
	for(std::list<align*>::iterator it= alignList.begin();it != alignList.end();++it){
		delete *it;
	}
}

